package com.foreknow.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.RegistLevel;
import com.foreknow.entity.SettleCategory;
import com.foreknow.service.RegistLevelService;
import com.foreknow.service.SettleCategoryService;
import com.foreknow.utils.Constance;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "SettleCategoryController", description = "结算级别管理")
@RestController
@RequestMapping(value = "/SettleCategoryController")
public class SettleCategoryController {
    @Autowired
    private SettleCategoryService settleCategoryService;

    @ApiOperation("获取结算级别列表")
    @RequestMapping(value = "/quetyAll",method = RequestMethod.GET)
    public String quetyAll(@RequestParam Map<String,Object> para) throws JsonProcessingException {
        //获取参数
        String param = (String)para.get("query");
        SettleCategory s=new SettleCategory();
        s.setSettlecode(param);
        s.setSettlename(param);
        //获取分页需要的参数
        Integer pageNum =Integer.parseInt((String) para.get("pageNum"));
        Integer pageSize=Integer.parseInt((String) para.get("pageSize"));
        //设置分页信息
        PageHelper.startPage(pageNum,pageSize);

        List<SettleCategory> list = settleCategoryService.quetyAll(s);
        //调用PageInfo
        PageInfo<SettleCategory> info = new PageInfo<>(list);
        HashMap<String, Object> hs = new HashMap<>();
        //给HashMap添加数据
        hs.put("totalpage",info.getTotal());
        hs.put("pagenum",info.getPageNum());
        hs.put("data", list);
        if (list != null) {
            hs.put("meta", new Constance("msg", 200));
        } else {
            hs.put("meta", new Constance("msg", 404));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/findSettleById",method = RequestMethod.GET)
    public String findSettleById(@RequestParam Integer id) throws JsonProcessingException {
        SettleCategory settleCategory = settleCategoryService.selectByPrimaryKey(id);
        HashMap<String, Object> hs = new HashMap<>();
        hs.put("data",settleCategory);
        if (settleCategory!=null){
            hs.put("meta",new Constance("msg",200));
        }else {
            hs.put("meta",new Constance("msg",403));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/modify",method = RequestMethod.PUT)
    public String modifySettle(@RequestBody SettleCategory user) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        int i = settleCategoryService.updateById(user);
        hs.put("data",i);
        if (i>0){
            hs.put("meta", new Constance("修改成功", 200));
        }else {
            hs.put("meta", new Constance("修改失败", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/insertSettle",method = RequestMethod.POST)
    public String insertSettle(@RequestBody Map<String,Object> para)throws JsonProcessingException {
        String registCode = (String)para.get("settlecode");
        String registName = (String)para.get("settlename");
        Integer sequenceNo =Integer.parseInt((String) para.get("sequenceno"));

        SettleCategory settleCategory = new SettleCategory();
        settleCategory.setSettlecode(registCode);
        settleCategory.setSettlename(registName);
        settleCategory.setSequenceno(sequenceNo);

        int i = settleCategoryService.insertUser(settleCategory);
        HashMap<String,Object> hs=new HashMap<>();
        hs.put("data",i);
        if (i>0){
            hs.put("meta",new Constance("添加用户成功",200));
        }else {
            hs.put("meta",new Constance("添加用户失败",403));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/deleteSettleById",method = RequestMethod.GET)
    public String deleteSettleById(@RequestParam Map<String,Object> para)throws JsonProcessingException {
        Integer id =Integer.parseInt((String) para.get("id"));
        HashMap<String,Object> hs=new HashMap<>();
        int i = settleCategoryService.deleteByPrimaryKey(id);
        hs.put("data",i);
        if (i>0){
            hs.put("meta",new Constance("删除用户成功",200));
        }else {
            hs.put("meta",new Constance("删除用户失败",403));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/deleteSettle",method = RequestMethod.POST)
    public String deleteSettle(@RequestBody Map<String,List<Integer>>ids) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        Integer settleByid = settleCategoryService.deleteSettleById(ids.get("list"));
        if(settleByid>0){
            hs.put("meta", new Constance("删除成功！"+settleByid+"条数据被删除", 200));
        }else{
            hs.put("meta", new Constance("删除失败", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
}
